import os
import cv2
import numpy

# 灰度图路径
png_path = '..\\datasets\\'
# rgb图像保存路径
database_path = '..\\datasets\\'

# cv2方法转换
for root, dirs, files in os.walk(png_path):
    for file in files:
        portion = os.path.splitext(file)[1]
        if portion == '.png':
            print("正在转换灰度图:" + file)
            src = cv2.imread(file, 0)
            src_RGB = cv2.cvtColor(src, cv2.COLOR_GRAY2RGB)
            cv2.imwrite(root, src_RGB)

# 手动填充通道转换
# for gh in png_name_list:
#     file = os.path.join(png_path, gh)
#     # 将灰度图转为rgb图像
#     b = cv2.imread(file)
#     L = b.convert('RGB')
#     rgb_arr = numpy.asarray(L)
#     rgb_image = cv2.fromarray(rgb_arr)
#     database_path = database_path + '\\' + gh + '.png'
#     cv2.imwrite(database_path, rgb_image)